package com.example.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.blog.domain.LoveList;
import com.example.blog.domain.Response;
import com.example.blog.dto.loveList.AdminSelectLoveListDto;
import com.example.blog.note.LogInfo;
import com.example.blog.note.NoToken;
import com.example.blog.note.Permissions;
import com.example.blog.service.impl.LoveListServiceImpl;
import com.example.blog.dto.loveList.DelLoveListDto;
import com.example.blog.dto.loveList.LoveListDto;
import com.example.blog.dto.publicDto.QuerySearchDto;
import com.example.blog.dto.loveList.UpdateStatueLoveListDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;
import java.util.Optional;

@RestController
@Transactional
@Validated
@RequestMapping("/loveList")
public class LoveListController {
    private final LoveListServiceImpl loveListService;
    @Autowired
    public LoveListController(LoveListServiceImpl loveListService){
        this.loveListService=loveListService;
    }
    @NoToken
    @LogInfo("获取所有事项")
    @GetMapping("/getAll")
    public Response getAll(){
        return new Response(Response.SUCCEED_CODE,"获取所有事项",loveListService.list());
    }

    //获取所有恋爱事项信息(后台管理)
    @Permissions("/loveList/getAll/admin")
    @LogInfo("查询恋爱事项信息")
    @PostMapping("/getAll/admin")
    public Response getAllAdmin( @RequestBody AdminSelectLoveListDto querySearchDto){
        IPage page = new Page(querySearchDto.getIndex(),querySearchDto.getPage());
        LambdaQueryWrapper<LoveList> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Objects.nonNull(querySearchDto.getStatue()),LoveList::getStatue,querySearchDto.getStatue())
                .like(Objects.nonNull(querySearchDto.getDateTime()),LoveList::getDateTime,querySearchDto.getDateTime())
                .like(Objects.nonNull(querySearchDto.getCondition()),LoveList::getContent, querySearchDto.getCondition())
                .orderByDesc(LoveList::getId);
        return new Response(Response.SUCCEED_CODE,"获取成功",loveListService.page(page,wrapper));
    }

    //新增恋爱事项
    @LogInfo("新增恋爱事项")
    @Permissions("/loveList/add")
    @PostMapping("/add")
    public Response add( LoveListDto loveList ){
        String img = loveListService.uploadImg(new MultipartFile[]{loveList.getFile()});
        LoveList list = new LoveList();
        list.setContent(loveList.getContent());
        list.setImg(img);
        return new Response(Response.SUCCEED_CODE,"删除成功",loveListService.save(list));
    }


   // 变更恋爱事项状态
    @LogInfo("变更恋爱事项状态")
    @Permissions("/loveList/updateStatue")
    @PostMapping("/updateStatue")
    public Response updateStatue(@RequestBody UpdateStatueLoveListDto loveListVo){
        UpdateWrapper<LoveList> wrapper = new UpdateWrapper<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        wrapper.set(loveListVo.getStatue()==1,"finish_time",format.format(new Date()))
                .set(loveListVo.getStatue()==0,"finish_time",null)
                .eq("id",loveListVo.getId()).set("statue",loveListVo.getStatue());
        return new Response(Response.SUCCEED_CODE,"变更成功",String.valueOf(loveListService.update(null,wrapper)));
    }


    //删除恋爱事项
    @LogInfo("删除恋爱事项")
    @Permissions("/loveList/delete")
    @PostMapping("/delete")
    public Response delete(@RequestBody DelLoveListDto delLoveListVo){
        Integer length = loveListService.delImg(delLoveListVo.getImg());
        if (length == 0){
            return new Response(Response.ERROR_CODE,"删除失败",null);
        }
        LambdaQueryWrapper<LoveList> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LoveList::getId,delLoveListVo.getId());
        return new Response(Response.SUCCEED_CODE,"删除成功",loveListService.remove(wrapper));
    }

}
